package com.lib.framework.base

import android.content.BroadcastReceiver
import android.content.Context
import android.content.IntentFilter

/**
 * 广播接收器 基类
 *
 * @author keanbin
 */
abstract class BaseBroadcastReceiver(context: Context): BroadcastReceiver() {

    protected val mTag = this.javaClass.canonicalName ?: "BaseBroadcastReceiver"
    protected val mContext = context.applicationContext
    private var isRegisterBroadReceiver: Boolean = false

    /**
     * 注册广播
     *
     * @return
     */
    fun register() {
        val intentFilter = getIntentFilter()
        mContext.registerReceiver(this, intentFilter)
        isRegisterBroadReceiver = true
    }

    /**
     * 注销广播
     *
     * @return
     */
    fun unregister() {
        if (isRegisterBroadReceiver) {
            mContext.unregisterReceiver(this)
            isRegisterBroadReceiver = false
        }
    }

    abstract fun getIntentFilter(): IntentFilter
}